package itsm.isperp.framework.print;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.junit.BeforeClass;
import org.junit.Test;


public class TestWordTemplate {

  private static WordTemplate template;
  private static final String DOCX_MODEL_PATH = "doc/model.docx";
  private static final String DOCX_FILE_WRITE = "doc/test2.docx";
  private static Map<String, String> map = new HashMap<String, String>();

  @BeforeClass
  public static void init() {
    File file = new File(DOCX_MODEL_PATH);
    FileInputStream fileInputStream = null;
    try {
      fileInputStream = new FileInputStream(file);
      template = new WordTemplate(fileInputStream);
    } catch (IOException exception) {
      exception.printStackTrace();
    }

    // 璁剧疆鍙傛暟
    map.put("title", "aa");
    map.put("user1", "aa");
    map.put("user2", "aa");
    map.put("text1", "aa");
    map.put("text2", "aa");
    map.put("cell1", "1");
    map.put("cell2", "aa");
    map.put("cell3", "aa");
    map.put("cell4", "aa");
    map.put("year", "2016");
    map.put("month", "04");
    map.put("day", "03");
  }

  public static void testReplaceTag() {
    init();
    template.replaceTag(map);
  }

  public static void main(String[] args) throws IOException {
    testReplaceTag();
    File file = new File(DOCX_FILE_WRITE);
    FileOutputStream out;
    try {
      out = new FileOutputStream(file);
      BufferedOutputStream bos = new BufferedOutputStream(out);
      template.write(bos);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @Test
  public void testWrite() {
    testReplaceTag();
    File file = new File(DOCX_FILE_WRITE);
    FileOutputStream out;
    try {
      out = new FileOutputStream(file);
      BufferedOutputStream bos = new BufferedOutputStream(out);
      template.write(bos);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}
